******************************************************************
*Input: FinTech/bank borrowers dataset
*Output: Stata graph with share of firms per rating
******************************************************************
clear 
	/* use saved workspace*/
	use "$output\ifp_scrapped_aggregated_industries_modified.dta", clear


gen bank = p2p == 0
gen ifp = p2p == 1

egen ratingint = group(rating)
replace ratingint = ratingint - 1
replace ratingint = 13 if ratingint==0
	
gen code = 1 if ratingint == 1
replace code = 2 if ratingint == 2
replace code = 3 if ratingint == 3
replace code = 4 if ratingint == 4
replace code = 5 if ratingint == 5
replace code = 6 if ratingint == 6
replace code = 7 if ratingint == 7
replace code = 8 if ratingint == 8
replace code = 9 if ratingint == 9
replace code = 10 if ratingint == 10
replace code = 11 if ratingint == 11 | rating == 12
replace code = 12 if ratingint == 13

drop if code == .

collapse (sum) ifp bank,by(code)

sort code
egen tot_ifp = total(ifp)
egen tot_bank = total(bank)

replace ifp = ifp/tot_ifp*100
replace bank = bank/tot_bank*100

label var ifp "Fintech borrowers"
label var bank "Bank borrowers"

graph hbar ifp bank , over(code, gap(*1)  ///
relabel(1 "3++"  ///
2 "3+" ///
3 "3" ///
4 "4+" ///
5 "4" ///
6 "5+" ///
7 "5" ///
8 "6" ///
9 "7" ///
10 "8" ///
11 "P/9" ///
12 "Unrated" ///
) ) ///
graphregion(color(white)) blabel(bar, format(%8.1f)   )nolabel ///
 bar(1, bcolor(emerald)) bar(2, bcolor(gs10%40)  lcolor(black))  ///
legend( region(lcolor(white) )  lcolor(white) label(1 "Fintech borrowers") label(2 "Bank borrowers") cols(2) size(medium) ) ylabel(,nogrid labsize(medium))
graph export "$figures\ratings.pdf", as(pdf) replace 
graph save "$figures\ratings.gph", replace 

